/*
 * Copyright (c) 2008 Anshuman Manur <anshuman.manur@gmail.com>,
 * Lavanya Shastri <lavanya.shastri@gmail.com>
 * 
 * This file is part of Andronova.
 * 
 * Andronova is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Andronova is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with Andronova.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This file may use the functionality provided by the library
 * jid3lib version 0.5.4 (<http://sourceforge.net/projects/javamusictag/>).
 * jid3lib and its uses are covered by GNU Lesser General Public License.
 * 
 */

package com.googlecode.andronova.helper;

import java.io.File;
import java.io.IOException;

import org.farng.mp3.TagException;
import org.farng.mp3.id3.AbstractID3v2;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PlayList {

	private SQLiteDatabase db;
	
	private Cursor c;
	
	private long size;
	
	private long maxId;
	
	public PlayList(SQLiteDatabase db) {
		this.db = db;
				
		setMaxId();
	}
	
	public long getSize() {
		return size;
	}
	
	public long getMaxId() {
		return maxId;
	}
	
	public void setMaxId() {
		c = db.query(DBConstants.PL_TABLE_NAME, null, null, null, null, null, DBConstants.CNAME_ID);
		if(c == null) return;
		size = c.count();
		if(size!=0) {
			c.last();
			maxId = c.getLong(c.getColumnIndex(DBConstants.CNAME_ID));
		}
		else
			maxId = -1;
	}
	
	public void clear() {
		db.delete(DBConstants.PL_TABLE_NAME, null, null);
		setMaxId();
	}
	
	public Cursor getCursor(String selection) {
		return db.query(DBConstants.PL_TABLE_NAME, null, selection, null, null, null, null);
	}
	
	public Song getSong(long id) throws IOException {
		Cursor c = db.query(DBConstants.PL_TABLE_NAME, null, DBConstants.CNAME_ID + "=\'" +  id + "\'", null, null, null, null);
		c.first();
		String path = c.getString(c.getColumnIndex(DBConstants.CNAME_PATH));
		Song s;
		try {
			s = new Song(path);
			long libId = c.getLong(c.getColumnIndex(DBConstants.CNAME_LIB_ID));
			s.setLibId(libId);
			s.setPlayListId(id);
			return s;
		}
		catch(TagException e) {
			return null;
		}
	}
	
	public int add(Song s) {
		AbstractID3v2 tag = s.getID3v2Tag();
	
		long libId = s.getLibId();
		File f = s.getMp3file();
		String path = f.getAbsolutePath();
		String title, artist, album;
		
		if(tag != null) {
			title = tag.getSongTitle();
			artist = tag.getLeadArtist();
			album = tag.getAlbumTitle();
		}
		else {
			title = f.getName();
			artist = "Unknown";
			album = "Unknown";
		}
		
		if(title.equals(""))
			title = f.getName();
		if(artist.equals(""))
			artist = "Unknown";
		if(album.equals(""))
			album = "Unknown";
		
		ContentValues cv = new ContentValues();
		
		cv.put(DBConstants.CNAME_LIB_ID, libId);
		cv.put(DBConstants.CNAME_PATH, path);
		cv.put(DBConstants.CNAME_TITLE, title);
		cv.put(DBConstants.CNAME_ARTIST, artist);
		cv.put(DBConstants.CNAME_ALBUM, album);
		
		long status = db.insert(DBConstants.PL_TABLE_NAME, null, cv);
		if(status == -1)
			return -1;
		setMaxId();
		return 1;
	}
	
	public void remove(Song s) {
		long id = s.getPlayListId();
		remove(id);
	}
	
	public void remove(long id) {
		db.delete(DBConstants.PL_TABLE_NAME, DBConstants.CNAME_ID + "=\'" + id + "\'", null);
		setMaxId();
	}
	
	public void move(long fromId, long toId) {
		ContentValues cv = new ContentValues();
		
		cv.put(DBConstants.CNAME_ID, -1);
		db.update(DBConstants.PL_TABLE_NAME, cv, DBConstants.CNAME_ID + "=\'" + fromId + "\'", null);
		
		cv.clear();
		cv.put(DBConstants.CNAME_ID, fromId);
		db.update(DBConstants.PL_TABLE_NAME, cv, DBConstants.CNAME_ID + "=\'" + toId + "\'", null);
		
		cv.clear();
		cv.put(DBConstants.CNAME_ID, toId);
		db.update(DBConstants.PL_TABLE_NAME, cv, DBConstants.CNAME_ID + "=\'-1\'", null);
	}
	
	public boolean hasId(long id) {
		Cursor c = db.query(DBConstants.PL_TABLE_NAME, null, DBConstants.CNAME_ID + "=\'" + id + "\'", null, null, null, null);
		if(c.count() == 0)
			return false;
		return true;
	}
	
	public boolean hasSong(Song s) {
		long libId = s.getLibId();
		Cursor c = db.query(DBConstants.PL_TABLE_NAME, null, DBConstants.CNAME_LIB_ID + "=\'" + libId + "\'", null, null, null, null);
		
		if(c.count() == 0)
			return false;
		return true;
	}
	
	public void update(Song s) {
		AbstractID3v2 tag = s.getID3v2Tag();
		long id = s.getPlayListId();
		
		String title, artist, album;
		
		title = tag.getSongTitle();
		artist = tag.getLeadArtist();
		album = tag.getAlbumTitle();
		
		if(title.equals(""))
			title = s.getMp3file().getName();
		if(artist.equals(""))
			artist = "Unknown";
		if(album.equals(""))
			album = "Unknown";
		
		ContentValues cv = new ContentValues();
		
		cv.put(DBConstants.CNAME_TITLE, title);
		cv.put(DBConstants.CNAME_ARTIST, artist);
		cv.put(DBConstants.CNAME_ALBUM, album);		
		
		db.update(DBConstants.PL_TABLE_NAME, cv, DBConstants.CNAME_ID + "=\"" + id + "\"", null);
	}
	
	@Override
	protected void finalize() throws Throwable {
		try {
			db.close();
		}
		finally {
			super.finalize();
		}
	}
}
